<?php

namespace backend\controllers;

use Yii;
use backend\models\Project;
use backend\models\ProjectSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use Da\User\Filter\AccessRuleFilter;
use yii\data\ActiveDataProvider;

/**
 * ProjectController implements the CRUD actions for Project model.
 */
class ProjectController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'ruleConfig' => [
                    'class' => AccessRuleFilter::class,
                ],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['index', 'view', 'view-donation-contract', 'view-contracts', 'view-smi', 'view-partners', 'view-documents', 'view-photos'],
                        'roles' => ['browseProject'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['create'],
                        'roles' => ['createProject'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['update'],
                        'roles' => ['updateProject'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['delete'],
                        'roles' => ['deleteProject'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Project models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel_Active = new ProjectSearch();
        $dataProvider_Active = $searchModel_Active->search(Yii::$app->request->queryParams, Project::STATUS_ACTIVE);

        $searchModel_Close = new ProjectSearch();
        $dataProvider_Close = $searchModel_Close->search(Yii::$app->request->queryParams, Project::STATUS_CLOSE);

        $searchModel_Planned = new ProjectSearch();
        $dataProvider_Planned = $searchModel_Planned->search(Yii::$app->request->queryParams, Project::STATUS_PLANNED);

        $searchModel_Idea = new ProjectSearch();
        $dataProvider_Idea = $searchModel_Idea->search(Yii::$app->request->queryParams, Project::STATUS_IDEA);

        return $this->render('index', [
            'searchModel_Active' => $searchModel_Active,
            'dataProvider_Active' => $dataProvider_Active,
            'searchModel_Close' => $searchModel_Close,
            'dataProvider_Close' => $dataProvider_Close,
            'searchModel_Planned' => $searchModel_Planned,
            'dataProvider_Planned' => $dataProvider_Planned,
            'dataProvider_Idea' => $dataProvider_Idea,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $contractDataProvider = new ActiveDataProvider([
            'query' => $model->getContracts(),
            'pagination' => false,
        ]);

        return $this->render('view', [
            'model' => $model,
            'contractDataProvider' => $contractDataProvider,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewDonationContract($id)
    {
        $model = $this->findModel($id);

        $donationDataProvider = new ActiveDataProvider([
            'query' => $model->getDonations(),
            'pagination' => false,
        ]);

        return $this->render('viewDonationContract', [
            'model' => $model,
            'dataProvider' => $donationDataProvider,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewSmi($id, $id_smi = '')
    {
        $model = $this->findModel($id);

        if($id_smi != '')
        {
            $modelSmi = \backend\models\ProjectSmi::findOne($id_smi);
        }
        else
        {
            $modelSmi = new \backend\models\ProjectSmi();
            $modelSmi->id_project = $model->id;
        }
        
        $smiDataProvider = new ActiveDataProvider([
            'query' => $model->getSmi(),
            'pagination' => false,
        ]);


        if ($modelSmi->load(Yii::$app->request->post())) 
        {
            if($modelSmi->validate())
            {
                if($modelSmi->save(false))
                {
                    $modelSmi = new \backend\models\ProjectSmi();
                    $modelSmi->id_project = $model->id;
                }
            }
        }

        return $this->render('viewSmi', [
            'model' => $model,
            'modelSmi' => $modelSmi,
            'smiDataProvider' => $smiDataProvider,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewContracts($id)
    {
        $model = $this->findModel($id);
        $contractDataProvider = new ActiveDataProvider([
            'query' => $model->getContracts(),
            'pagination' => false,
        ]);

        return $this->render('viewContracts', [
            'model' => $model,
            'contractDataProvider' => $contractDataProvider,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewPartners($id)
    {
        $model = $this->findModel($id);

        return $this->render('view-partners', [
            'model' => $model,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewDocuments($id)
    {
        $model = $this->findModel($id);

        return $this->render('view-documents', [
            'model' => $model,
        ]);
    }

    /**
     * Displays a single Project model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionViewPhotos($id)
    {
        $model = $this->findModel($id);

        return $this->render('view-photos', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new Project model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Project();
        $model->is_proxy = Project::OWN;

        if ($model->load(Yii::$app->request->post())) 
        {
            if($model->validate())
            {
                if($model->save(false))
                {
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Project model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) 
        {
            if($model->validate())
            {
                if($model->save(false))
                {
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Project model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Project model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Project the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Project::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
